package com.lmu.travel.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lmu.travel.dao.RouteDao;
import com.lmu.travel.entity.Route;
import com.lmu.travel.entity.WeatherInfo;
import com.lmu.travel.service.RouteService;
import com.lmu.travel.util.HttpURLUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @author liuxuhuang
 * @className: RouteServiceImpl
 * @description: 路线sericeImpl
 * @email liuxuhuang3970@qq.com
 * @date 2021/6/28 19:09
 * @version：1.0
 */
@Service
public class RouteServiceImpl extends ServiceImpl<RouteDao, Route> implements RouteService {
    @Autowired
    private RouteDao routeDao;

    @Override
    public WeatherInfo getWeatherInfo(Integer districtid) {
        String ak = "MtjwFTPRv1e3sdod9yitQotm2gaZYNF5";
        String url = "http://api.map.baidu.com/weather/v1/?district_id=" + districtid + "&data_type=all&ak=" + ak;
        String s = HttpURLUtil.doGet(url);
        JSONObject jsonObject = JSON.parseObject(s, JSONObject.class);
        if (jsonObject.getInteger("status") != null && jsonObject.getInteger("status") == 0) {
            JSONObject result = jsonObject.getJSONObject("result");
            JSONObject now = result.getJSONObject("now");
            JSONArray forecasts = result.getJSONArray("forecasts");
            return new WeatherInfo(now, forecasts);
        }
        return null;
    }

    @Override
    public void updateRouteCount(String rid) {
        routeDao.updateRouteCount(rid);
    }

    @Override
    public List<Route> selectHot() {
        return routeDao.selectHot();
    }
}
